﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FF.Entity;
using System.Windows.Forms;
using System.Data.SQLite;
using System.Data;

namespace FF.Method
{
    class GoodMethod
    {
        #region //生成编号 例如：KH-20071118114255.此方法可公用。
        public static string generateID(string header)
        {
            int intYear = DateTime.Now.Day;
            int intMonth = DateTime.Now.Month;
            int intDate = DateTime.Now.Year;
            int intHour = DateTime.Now.Hour;
            int intSecond = DateTime.Now.Second;
            int intMinute = DateTime.Now.Minute;
            string strTime = null;
            strTime = intYear.ToString();
            if (intMonth < 10)
            {
                strTime += "0" + intMonth.ToString();
            }
            else
            {
                strTime += intMonth.ToString();
            }
            if (intDate < 10)
            {
                strTime += "0" + intDate.ToString();
            }
            else
            {
                strTime += intDate.ToString();
            }
            if (intHour < 10)
            {
                strTime += "0" + intHour.ToString();
            }
            else
            {
                strTime += intHour.ToString();
            }
            if (intMinute < 10)
            {

                strTime += "0" + intMinute.ToString();
            }
            else
            {
                strTime += intMinute.ToString();
            }
            if (intSecond < 10)
            {

                strTime += "0" + intSecond.ToString();
            }
            else
            {
                strTime += intSecond.ToString();
            }


            return (header + strTime);



        }// end if 
        #endregion

        public static int deleteGood(string GoodId)
        {
            try
            {
                string sql = "delete from ff_good where good_id='" + GoodId + "'";
                return SQLiteHelper.ExecuteSql(sql);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }
           
        }

        public static int addGood(Good good)
        {
            string sql = "insert into ff_good(name,price1,price2,price3,remark,add_time,add_user_id,add_username) values('";
            //sql += good.goodId + "','";
            sql += good.name + "','";
            sql += good.price1 + "','";
            sql += good.price2 + "','";
            sql += good.price3+ "','";
            sql += good.remark + "','";
            sql += good.addTime + "','";
            sql += good.addUserId + "','";
            sql += good.addUserName + "')";
            try{
                return SQLiteHelper.ExecuteSql(sql);
            }catch(Exception ex)
            {

                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }
        }

        public static int updateGood(Good good)
        {
            string sql = "update ff_good set name='" +  good.name + "',";
            sql += "price1='" + good.price1 + "',";
            sql += "price2='" + good.price2 + "',";
            sql += "price3='" + good.price3 + "',";
            sql += "add_username='" + good.addUserName + "',";
            sql += "remark='"+ good.remark + "' where good_id='" + good.goodId + "'";
            try
            {
                return SQLiteHelper.ExecuteSql(sql);
            }
            catch (Exception ex)
            {

                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return 0;
            }
        }

        public static DataSet getGoodByKeyword(string keyword)
        {
            string sql = "select a.good_id,a.name,price1,price2,price3,a.remark,strftime('%Y-%m-%d',add_time) as add_time ,a.add_username as user_name from ff_good a where  a.name like '%" + keyword + "%'";
            try
            {
                return SQLiteHelper.Query(sql);
            }
            catch (System.Exception ex)
            {
            	MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }


        internal static Good getGoodById(string selectedGoodId)
        {
            Good good = null;
            try
            {
                SQLiteDataReader reader = SQLiteHelper.ExecuteReader("select * from ff_good where good_id='" + selectedGoodId + "'");
                if (reader.HasRows)
                {
                    reader.Read();
                    good = new Good();
                    good.goodId = reader.GetInt32(0).ToString();
                    good.name = reader.GetString(1);
                    good.price1 = reader.GetString(2);
                    good.price2 = reader.GetString(3);
                    good.price3 = reader.GetString(4);
                    good.remark = reader.GetString(5);
                    good.addTime = reader.GetString(6);
                    good.addUserId = reader.GetString(7);
                    good.addUserName = reader.GetString(8);
                }
                reader.Close();
                return good;
            }
            catch (Exception ex)
            {
                MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        public static void fillGood(object objTreeView, object obimage)
        {
            if (objTreeView.GetType().ToString() == "System.Windows.Forms.TreeView")
            {
                System.Windows.Forms.ImageList imlist = (System.Windows.Forms.ImageList)obimage;
                System.Windows.Forms.TreeView TV = (System.Windows.Forms.TreeView)objTreeView;
                TV.Nodes.Clear();
                TV.ImageList = imlist;
                try
                {
                    SQLiteDataReader readerType = SQLiteHelper.ExecuteReader("select * from ff_good");
                    //SQLiteDataReader rederUser;
                    if (readerType.HasRows)
                    {
                        System.Windows.Forms.TreeNode TN;
                        while (readerType.Read())
                        {
                            string name = readerType.GetString(1);
                            TN = TV.Nodes.Add(readerType.GetInt32(0).ToString(), readerType.GetString(1), 1, 0);

                            TN.Nodes.Add(readerType.GetInt32(0).ToString(), name + "|" + readerType.GetString(2), 1, 0);//price1
                            TN.Nodes.Add(readerType.GetInt32(0).ToString(), name + "|" + readerType.GetString(3), 1, 0);//price2
                            TN.Nodes.Add(readerType.GetInt32(0).ToString(), name + "|" + readerType.GetString(4), 1, 0);//price3
                            //rederUser = SQLiteHelper.ExecuteReader("select user_id, name from ff_user where type='" + readerType.GetString(0) + "'");
                            //if (rederUser.HasRows)
                            //{
                            //    while (rederUser.Read())
                            //    {
                            //        //TreeNode newNode12 = new TreeNode(rederUser.GetString(0), 1, 0);
                            //        //newNode12.Nodes.Add("A", qlddr[4].ToString(), 0, 1);
                            //        //TN.Nodes.Add(newNode12);
                            //        TN.Nodes.Add(rederUser.GetString(0), rederUser.GetString(1), 1, 0);
                            //    }
                            //}
                            //rederUser.Close();

                            //System.Windows.Forms.TreeNode TN = TV.Nodes.Add("B", "商品B", 1, 0);

                            // System.Windows.Forms.TreeNode TN = TV.Nodes.Add("C", "商品C", 1, 0);

                            //TreeNode newNode12 = new TreeNode("草果ID", 1, 0);
                            //newNode12.Nodes.Add("A", "苹果", 0, 1);
                            //TN.Nodes.Add(newNode12);


                        }
                        TV.ExpandAll();

                    }
                    readerType.Close();
                }
                catch (System.Exception ex)
                {
                    MessageBox.Show("程序出错了！" + ex.Message, "提示信息 ", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

            }
        }
    }
}
